/*    

    bobbietalk - a visual programming environment for toy robotics

    Copyright (C) 2013  Roberto Tiella

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/
package bobbietalk.simulator;

import bobbietalk.simulator.sensors.Sensor;
import bobbietalk.simulator.world.World;
import java.awt.Shape;
import org.apache.log4j.Logger;

/**
 * An abstract robot platform: it lacks the body
 *
 * motion model is based on two indipent motors, each one controlling a weel
 * each weel is connected to a ticker counter. when the body hits an obstacle
 * the robots goes in the opposite until it exit the collision and then it
 * stops.
 *
 * @author tiella
 */
public abstract class AbstractSimpleRobot extends AbstractRobot {

    private Logger log = Logger.getLogger(this.getClass());
    protected SimpleMotorsPlatform motors;

    /**
     */
    public AbstractSimpleRobot() {


        motors = new SimpleMotorsPlatform(this);

        // motors.init();

    }

    protected void initMotors() {
        motors.init();
    }

    public void rightMotorOff() {
        motors.rightMotorOff();
    }

    public void rightMotorForward() {
        motors.rightMotorForward();
    }

    public void rightMotorBackward() {
        motors.rightMotorBackward();
    }

    public void leftMotorOff() {
        motors.leftMotorOff();
    }

    public void leftMotorForward() {
        motors.leftMotorForward();
    }

    public void leftMotorBackward() {
        motors.leftMotorBackward();
    }

    public int getTickR() {
        return motors.getTickR();
    }

    public int getTickL() {
        return motors.getTickL();
    }

    @Override
    public void setPlatformActive(boolean v) {
//        System.out.println("******** Platform active **********");
    }

    public void setRightMotorSpeed(double speed) {
        motors.setRightMotorSpeed(speed);
    }

    public void setLeftMotorSpeed(double speed) {
        motors.setLeftMotorSpeed(speed);
    }

    public SimpleMotorsPlatform getMotors() {
        return motors;
    }

    @Override
    public void addObserver(RobotObserver obs) {
        motors.addObserver(obs);
    }

    public void setSpeed(double defautSpeed) {
        setLeftMotorSpeed(defautSpeed);
        setRightMotorSpeed(defautSpeed);
    }

    abstract public Sensor getSensor(String name);
//        throw new UnsupportedOperationException("Not yet implemented");
    // }

    public void setMotorSpeeds(double sl, double sr) {
        motors.setMotorSpeeds(sl, sr);
    }

    @Override
    public double getX() {

        return motors.getPosition().x;

    }

    @Override
    public double getY() {

        return motors.getPosition().y;

    }

    @Override
    public double getAngle() {

        return motors.getAngle();

    }

    @Override
    public void setDt(double dt) {

        motors.setDt(dt);

    }

    public abstract Shape getShape();

    public abstract void setWorld(World w);
}
